package com.liukai.bishe.presenter.userPresenter.impl;

import android.content.Context;
import android.text.format.DateFormat;

import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.liukai.bishe.presenter.userPresenter.IUploadToOSSPresenter;
import com.liukai.bishe.utils.Constants;
import com.liukai.bishe.utils.HashUtil;
import com.liukai.bishe.utils.ToastUtil;
import com.liukai.bishe.view.IBaseCallBack;

import java.io.File;
import java.util.Date;

public class IUploadToOSSPrensterImpl implements IUploadToOSSPresenter {

    private Context mContext;

    public IUploadToOSSPrensterImpl(Context context) {
        this.mContext = context;
    }

    @Override
    public OSS getOSSClient() {
        OSSCredentialProvider credentialProvider =
                new OSSPlainTextAKSKCredentialProvider(Constants.ACCESSKEYID ,
                        Constants.ACCESSKEYSERCRET);
        return new OSSClient(mContext, Constants.ENDPOINT, credentialProvider);
    }

    /**
     * 上传方法
     *
     * @param objectKey 标识
     * @param path      需上传文件的路径
     * @return 外网访问的路径
     */
    @Override
    public String upload(String objectKey, String path) {
        // 构造上传请求
        PutObjectRequest request =
                new PutObjectRequest(Constants.BUCKET_NAME,
                        objectKey, path);
        try {
            //得到client
            OSS client = getOSSClient();
            //上传获取结果
            PutObjectResult result = client.putObject(request);
            //获取可访问的url
            String url = client.presignPublicObjectURL(Constants.BUCKET_NAME, objectKey);
            //格式打印输出
            ToastUtil.log(mContext,String.format("PublicObjectURL:%s", url));
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 上传普通图片
     *
     * @param path 本地地址
     * @return 服务器地址
     */
    @Override
    public String uploadImage(String path) {
        String key = getObjectImageKey(path);
        return upload(key, path);
    }

    /**
     * 上传头像
     *
     * @param path 本地地址
     * @return 服务器地址
     */
    @Override
    public String uploadPortrait(String path) {
        String key = getObjectPortraitKey(path);
        return upload(key, path);
    }

    /**
     * 上传audio
     *
     * @param path 本地地址
     * @return 服务器地址
     */
    @Override
    public String uploadAudio(String path) {
        String key = getObjectAudioKey(path);
        return upload(key, path);
    }

    /**
     * 获取时间
     *
     * @return 时间戳 例如:201805
     */
    @Override
    public String getDateString() {
        return DateFormat.format("yyyyMM", new Date()).toString();
    }

    /**
     * 返回key
     *
     * @param path 本地路径
     * @return key
     */
    //格式: image/201805/sfdsgfsdvsdfdsfs.jpg
    @Override
    public String getObjectImageKey(String path) {
        String fileMd5 = HashUtil.getMD5String(new File(path));
        String dateString = getDateString();
        return String.format("image/%s/%s.jpg", dateString, fileMd5);
    }

    //格式: portrait/201805/sfdsgfsdvsdfdsfs.jpg
    @Override
    public String getObjectPortraitKey(String path) {
        String fileMd5 = HashUtil.getMD5String(new File(path));
        String dateString = getDateString();
        return String.format("portrait/%s/%s.jpg", dateString, fileMd5);
    }

    //格式: audio/201805/sfdsgfsdvsdfdsfs.mp3
    @Override
    public String getObjectAudioKey(String path) {
        String fileMd5 = HashUtil.getMD5String(new File(path));
        String dateString = getDateString();
        return String.format("audio/%s/%s.mp3", dateString, fileMd5);
    }

    @Override
    public void registerCallback(IBaseCallBack iBaseCallBack) {

    }

    @Override
    public void unregisterCallback(IBaseCallBack iBaseCallBack) {

    }
}
